<!DOCTYPE html>
<html class="x-admin-sm">
<head>
    {include file="public/static" /}
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">

                <div class="layui-card-header">
                    {eq name="check.AdminmessageRead" value="1"}
                        <button class="layui-btn" onclick="dataCheckboxAll(1)">
                            <i class="layui-icon">&#xe621;</i>标为已读
                        </button>
                    {/eq}
                    {eq name="check.AdminmessageDelAll" value="1"}
                        <button class="layui-btn layui-btn-danger" onclick="dataCheckboxAll(0)">
                            <i class="layui-icon">&#xe640;</i>批量删除
                        </button>
                    {/eq}
                </div>
                <div class="layui-card-body layui-table-body">
                    <table class="layui-table layui-form">
                        <thead>
                        <tr>
                            <th>
                                <input type="checkbox" lay-filter="checkbox" name="id" value="" lay-skin="primary">
                            </th>
                            <th>内容</th>
                            <th>状态</th>
                            <th>时间</th>
                        </thead>
                        <tbody>
                            {volist name="info" id="vo"}
                                <tr>
                                    <td>
                                        <input type="checkbox" name="id[]" value="{$vo.bm_id}" lay-skin="primary">
                                    </td>
                                    <td>
                                        <a href="JavaScript:;" data-title="{$vo.bm_title}" data-href="{$vo.bm_url}" onclick="locationMessage('{$vo.bm_id}', '{$vo.bm_isRead}', this)" >{$vo.bm_content}</a>
                                    </td>
                                    <td id="td{$vo.bm_id}">
                                        {eq name="vo.bm_isRead" value="1"}
                                            <span class="layui-badge layui-bg-green">已阅读</span>
                                        {else/}
                                            <span class="layui-badge">未阅读</span>
                                        {/eq}
                                    </td>
                                    <td>{$vo.bm_time|date='Y-m-d H:i:s'}</td>
                                </tr>
                            {/volist}
                        </tbody>
                    </table>
                </div>
                <div class="layui-card-body ">
                    <div class="page">
                        {$info|raw}
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
<script>
    /*消息链接跳转并阅读*/
    function locationMessage(id, read, that) {
        //跳转链接
        var title = $(that).attr('data-title');
        var href = $(that).attr('data-href');
        //如果已阅读
        if(read == 1){
            openWindow(title, href)
        }else{
            $.ajax({
                type: 'POST',//提交方式 post 或者get
                url: "{:url('Admin/messageRead')}",//提交到那里
                data: {id: id},
                success:function(data){
                    if(data.status == 1){
                        $("#td"+id).html('<span class="layui-badge layui-bg-green">已阅读</span>');
                        if(href != '#') openWindow(title, href) //打开tab窗口
                    }
                },
                error:function(data){
                    layer.close(index);
                }
            });
        }
    }
    /*打开tab窗口并关闭弹出层*/
    function openWindow(title, href){
        //打开tab窗口
        xadmin.add_tab(title, href);
        //关闭弹出层
        common.closeWindowReload();
    }
    /*全选*/
    common.selectCheckbox('checkbox','id[]');
    /*批量删除 or 阅读*/
    function dataCheckboxAll(status) {
        //获取checkbox值
        var ids = common.getCheckboxVal();
        if(ids.length < 1){
            layer.msg('请选择需要操作的数据！',{time:1500, icon:5});
            return false;
        }
        if(status == 1){
            var msg = "确认要标记选中数据为已读吗？";
            var url = "{:url('Admin/messageReadAll')}"
        }else{
            var msg = "确认要删除选中的数据吗？";
            var url = "{:url('Admin/messageDelAll')}"
        }
        layer.confirm(msg,function(){
            var index = layer.load();
            $.ajax({
                type: 'POST',
                url: url,
                data: {ids:ids},
                dataType:'json',
                success:function(data){
                    if(data.status === 1){
                        layer.msg(data.info,{time:1500, icon:6},function(){
                            window.location.reload();
                        });
                    }else{
                        layer.msg(data.info,{time:1500, icon:5});
                    }
                    layer.close(index);
                },
                error:function(){
                    layer.msg("不好意思，出现了一点问题", {icon: 5});
                }
            });
        });
    }
</script>
</html>